//
//  ZJRoomModelDetailVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/28.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJRoomModelDetailVC.h"

@interface ZJRoomModelDetailVC ()<UICollectionViewDelegate,UICollectionViewDataSource,MWPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
/**<数据源>*/
@property (nonatomic,strong) NSArray *dataArr;
/**<图片数组>*/
@property (nonatomic,strong) NSMutableArray *photoArr;
@end

@implementation ZJRoomModelDetailVC
-(NSMutableArray *)photoArr{
    
    if (!_photoArr ) {
        _photoArr = [NSMutableArray new];
    }
    return _photoArr;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@样板间",self.navTitle];
    [self setupCollectionLayout];
    [self sendRequest];
}

-(void)setupCollectionLayout{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小
    if (SCREEN_WIDTH == 375) {
        layout.itemSize = CGSizeMake(SCREEN_WIDTH/2.18, SCREEN_HEIGHT/3.5);
    }
    if (SCREEN_WIDTH == 320) {
        layout.itemSize = CGSizeMake(SCREEN_WIDTH/2.21, SCREEN_HEIGHT/3.5);
    }
    if (SCREEN_WIDTH == 414) {
        layout.itemSize = CGSizeMake(SCREEN_WIDTH/2.17, SCREEN_HEIGHT/3.5);
    }
    if (SCREEN_WIDTH > 414) {
        layout.itemSize = CGSizeMake(SCREEN_WIDTH/2.18, SCREEN_HEIGHT/3.5);
    }
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    self.collectionView.collectionViewLayout = layout;
}
#pragma mark - ******************请求数据
-(void)sendRequest{
    [HUD showLoading:@"加载数据中..."];
    NSDictionary *parameter = [LRNetwork requestParameter:@{@"id":self.model_id}];
    NSString *url = [LRNetwork requestURL:@"cgi/model_room_picture.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        self.dataArr = returnValue[@"picture"];
      
        [self.collectionView reloadData];
        [HUD hide];
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}
#pragma mark -UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary * dict = self.dataArr[indexPath.row];
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
     [imgV sd_setImageWithURL:[NSURL URLWithString:dict[@"pic"]?:@""] placeholderImage:[UIImage imageNamed:kDefaultImageName]];
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImg:)];
    [imgV addGestureRecognizer:tapGesturRecognizer];
    imgV.contentMode = UIViewContentModeScaleAspectFill;
    imgV.clipsToBounds = YES;
    imgV.tag = indexPath.row;
    imgV.userInteractionEnabled = YES;
    
    [self.photoArr addObject:dict[@"pic"]];
    [cell addSubview:imgV];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
// 点击图片
-(void)tapImg:(UITapGestureRecognizer *)tapGesturRecognizer{
    
    UITapGestureRecognizer * tap = (UITapGestureRecognizer *)tapGesturRecognizer;
    NSInteger index = tap.view.tag;
    
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    // Set options
    browser.displayActionButton = NO;
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    [browser setCurrentPhotoIndex:index];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    
    nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:nc animated:YES completion:nil];
    
}
#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
{
    return self.photoArr.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
{
    return [MWPhoto photoWithURL:[NSURL URLWithString:self.photoArr[index]]];
}

- (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
@end
